/**
 * 渠道级任务组管理js文件
 * 
 * 参数 params { 
 * 		callback   ：{ 这里可传ztree api上所有的回调 },
 *      renderTo   : 树要追加的dom id url : 数据请求的url
 *      title      : '',
 *      url        :{add : 'url', query : 'url'}
 *     }
 * 
 * @author fb
 */
window.mofang.core.group = function(_params) {
	var params = _params;
	var ztree = null;
	var treeCache = null;

	var show = function() {
		// 如果onRightClick回调不存在，则使用回认回调方法
		params.callback || (params.callback = {});
		params.callback.onRightClick || (params.callback.onRightClick = onRightClick);
		

		treeInit();
		eventInit();
	};

	var treeInit = function() {
		treeCache = {};
		util.mofangAjax({
			url : params.url.query,
			succFun : function(json) {
				// 数据格式转化iconSkin:'icon-process',
				var top = {
					name : params.title + "根结点",
					open : true,
					children : []
				};
				$.each(json.data, function(i, data) {
					var node = {};
					node['id'] = data.id;
					node['name'] = data.name;
					treeCache[data.id] = data;

					if (data.children && data.children.length > 0) {
						node['open'] = true;
						node['children'] = [];
						dataFormat(data.children, node);
					}

					top.children.push(node);
				});

				// ztree控件
				var setting = {
					view : {
						dblClickExpand : function(treeId, treeNode) {
							return treeNode.level > 0;
						}
					},
					callback : params.callback

				};

				ztree = $.fn.zTree.init($('#' + params.renderTo), setting, [ top ]);
			}
		});
	};

	var dataFormat = function(childrens, parent) {
		$.each(childrens, function(i, children) {
			var node = {};
			node['id'] = children.id;
			node['name'] = children.name;
			treeCache[children.id] = children;

			if (children.children && children.children.length > 0) {
				node['open'] = true;
				node['children'] = [];
				dataFormat(children.children, node);
			}

			parent.children.push(node);
		});
	};
	
	var eventInit = function() {
		var hideSetTime = null;
		$('#rMenu').unbind('mouseout').bind('mouseout',function(e){
			var _this = $(this);
			hideSetTime = setTimeout(function(){
				_this.hide();
			},500);
		}).unbind('mouseover').bind('mouseover',function(e){
			if (hideSetTime) {
				clearTimeout(hideSetTime);
			}
		});
	};

	var command = {
		'add' : function(treeNode) {
			new webBox({
				title 	: params.title + '添加',
				url   	: 'pages/core/group-addOrUpdate.jsp',
				saveFun : function(closeFun) {
					var form = $('#groupForm');
					if (!Validator.Validate(form[0], 3)) return;
					var data = util.serializeJson(form);
					
					if (treeNode.level > 0) {
						$.extend(data, {'parant.id' : treeNode.id});
					}
					
					util.mofangAjax({
						url 	: params.url.add,
						data 	: data,
						succFun : function(json) {
							$.Prompt(json.result);
							treeInit();
							closeFun();
						}
					});
				}
			}).init().show(function(){
				$('#groupForm').find("input[name^='sn']").val(new UUID());
			});
		},
		'update' : function(treeNode) {
			new webBox({
				title 	: params.title + '修改',
				url   	: 'pages/core/group-addOrUpdate.jsp',
				saveFun : function(closeFun) {
					var form = $('#groupForm');
					if (!Validator.Validate(form[0], 3)) return;
					// 更新
					var data = util.serializeJson(form);
					data['id'] = treeNode.id;
					
					util.mofangAjax({
						url 	: params.url.update,
						data 	: data,
						succFun : function(json) {
							$.Prompt(json.result);
							treeInit();
							closeFun();
						}
					});
				}
			}).init().show(function(){
				$('#groupForm').find('input').each(function() {
					this.value = treeCache[treeNode.id][this.name];
				});
			});
		},
		'delete' : function(treeNode) {
			$(this).confirm({
				title   : '系统提示',
				content : '些操作将会删除 与之相关联的所有数据，确定要该【'+treeNode.name+'】组吗?',
				callBack : function() {
					util.mofangAjax({
						url 	: params.url.del,
						data 	: {ids : [treeNode.id]},
						succFun : function(json) {
							$.Prompt(json.result);
							treeInit();
						}
					});
				}
			});
		}
	};

	var onRightClick = function(event, treeId, treeNode) {
		if (treeNode) {
			var rMenu = $('#rMenu').show();
			rMenu.css({
				"top" : event.clientY + "px",
				"left" : event.clientX + "px",
				"visibility" : "visible"
			});
			
			ztree.selectNode(treeNode);

			rMenu.find('li').unbind('click').bind('click', function(e) {
				rMenu.hide();
				var action = $(this).attr('action');
				
				if (treeNode.level < 1 && action != 'add') {
					$.Prompt('根结点不能进行此操作!');
					return
				}
				
				command[action](treeNode);
				return util.stopDefault(e);
			});
		}
	};

	return {
		show : show
	};
};